package com.nbgs.demo.controller;

import com.nbgs.demo.common.EncryptUtil;
import com.nbgs.demo.common.Result;
import com.nbgs.demo.controller.form.Form;
import com.nbgs.demo.dao.AuthMapper;
import com.nbgs.demo.model.Worker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class AddUserController {
    @Autowired
    private AuthMapper authMapper;
    @PostMapping(value = "/add")
    public Result addUser(Form form){
        log.info("注册:{}",form);
        System.out.println(form.getWorkerNum());
        System.out.println(form.getWorkerPassword());
//        EncryptUtil.encrypt(form.getWorkerPassword());
        List<Worker> workers=authMapper.findAll();
        for (Worker worker:workers) {
            if (worker.getWorkerName().equals(form.getWorkerNum())){
                return Result.fail("用户名已存在");
            }
        }
        form.setWorkerPassword( EncryptUtil.encrypt(form.getWorkerPassword()));
        authMapper.addWorker(form);
        return Result.success("注册成功");
    }
}
